<?php
namespace app\mobile\controller;

use app\model\Record; 
use app\model\Util; 
use app\model\Theme; 
use app\model\Order; 

class Autorefund extends Base
{
    public function Autorefund()
    {
global $_G, $_P;
$fans = $this->checkuser();
$system    = $_G['system'];
$op        = !empty($_P['op']) ? $_P['op'] : 'display';

$themeid = intval($_P['themeid']);
$theme = Theme::getThemeById($themeid);
$config =   Theme::getConfig($themeid);

if($op == 'auto' && $theme['isautorefund']== 1){

    $refundno = getMillisecond();
    if (!empty($theme['jiezhang'])){
        //return_msg('此活动已结算,无法退款');
        exit;
    }
    $totalpayfee = Order::getRealByThemeIdnew($themeid) + $theme['chongzhi'];
    if ($totalpayfee<1){
        //return_msg('您的余额不足,无法退款,需先充值再退款');
        exit;
    }

    if (intval($config['groupneedtime'])>0){
        $refundtime=strtotime('-'.intval($config['groupneedtime']).' hour');
        //echo date("Y-m-d H:i:s",$refundtime);
    }else{
        exit;
    }
    $sql = "SELECT * FROM " . tablename ( t_record ) . "
                WHERE themeid ='{$themeid}' and status > 0 
                and status!=3 and isfinish = 0 and isrefund = 0 and type != 9 and addtime < ".$refundtime."
                order by addtime
                LIMIT 1";
    $list = db_find ($sql);
    if (!empty($list)){
        $id = $list['id'];
    }else{
        exit;
    }
    $trade = Order::getPayOrderByRIdZhifude($id);
    if (empty($trade)){
        //return_msg('此定单未支付或已退');
        exit;
    }

    $ret = Util::applyrefund($trade,$refundno,$system);
    if ($ret['code']==0){
        $state = db_update(t_order, array('paystate' => -1), array('id' => $trade['id']));
        $tuanzhang = Record::getPayedGroupById($id,$themeid);
        $state = db_update(t_record, array('status' => -1,'isrefund'=>2), array('id' => $id));
        $state = db_update(t_record, array('isfinish' => 0), array('pid' => $id));

        $sql = "SELECT * FROM " . tablename (t_record) . " WHERE themeid =:themeid and id = :id";
        $getworker = db_find($sql, array(":themeid"=>$themeid,":id"=>$id));

        if (!empty($getworker)){
            db_query("UPDATE ".tablename(t_worker)." SET paycount = paycount - 1 WHERE themeid = '{$themeid}' and uid ={$getworker['workerid']}");
        }
        
        if (empty($tuanzhang)){
            //return_tips('退款成功！', $url, 'success');
        }else{
            $sql = "SELECT ord.* FROM " . tablename ( t_record ) . " as ord
                        WHERE  ord.themeid ='{$themeid}' 
                        and status = 1 and type = 1 and ord.pid='{$id}' 
                        ORDER BY ord.addtime asc ";
            $list = db_find ( $sql,$params);
            if (empty($list)){
                //return_tips('退款成功！', $url, 'success');
            }else{
                $state = db_update(t_record, array('type' => 0), array('id' => $list['id']));
                $state = db_update(t_record, array('pid' => $list['id']), array('pid' => $id));
                $state = db_update(t_order, array('ordertype' => 0), array('masterid' => $list['id'],'ordertype' => 1));
                $url = createWebUrl($do,array('op2' => 'scan','id' => $list['id'],'themeid'=>$themeid));
                //return_tips('退款成功！'.$list['username'].'成为新团长', $url, 'success');
            }
        }
    }else{
        $state = db_update(t_record, array('isrefund'=>3), array('id' => $id));
        //return_tips($ret['message'], $url, 'info');
    }

}

}
}